<?php
/**
 * Created by 1971599474@qq.com
 * User: zhixuan
 * Date: 2019/12/15
 * Time: 14:55
 */

namespace app\admin\controller;


use app\admin\validate\RoleValidate;
use app\common\utils\JsonService;
use app\admin\model\Role as RoleModel;
use app\admin\model\Privilege as PrivilegeModel;
use think\facade\View;

class Role extends Base
{

    /**
     * 添加角色
     * @return mixed
     */
    public function create(){
        if($this->request->isajax()){

            $post = $this->request->post();
            $privilege_id = $post['privilege_id'];
            $validate = new RoleValidate;
            if(!$validate->scene('add')->check($post)) {
                return JsonService::error($validate->getError());
            }
            try {
                $privilege = explode(',',$privilege_id);
                $privilege_id = array_unique($privilege);
                $post['privilege_id'] = implode(',',$privilege_id);
                RoleModel::create($post);

            } catch (\Exception $ex) {
                return JsonService::error($ex->getMessage());
            }
            return JsonService::success();
        }

        $privilegeData = PrivilegeModel::getTree();
        return View::fetch('create',['privilegeData'=>$privilegeData]);
    }

    /**
     * 编辑角色
     * @return mixed
     */
    public function update(){
        if($this->request->isAjax()){
            $post = $this->request->post();
            $privilege_id = $post['privilege_id'];
            $validate = new RoleValidate();
            if(!$validate->scene('edit')->check($post)) {
                return JsonService::error($validate->getError());
            }

            try {
                $privilege = explode(',',$privilege_id);
                $privilege_id = array_unique($privilege);
                $post['privilege_id'] = implode(',',$privilege_id);
                $res = RoleModel::updateOne($post,['id'=>$post['id']]);
            } catch (\Exception $ex) {
                return JsonService::error($ex->getMessage());
            }
            return JsonService::success();
        }

        $id = $this->request->get('id');
        $role = $this->findModel($id);
        $privilegeData = PrivilegeModel::getTree();
        return View::fetch('update',[
            'role'=>$role,
            'privilegeData'=>$privilegeData,
        ]);
    }

    /**
     * 角色列表
     * @return mixed
     */
    public function index(){
        $roleData = RoleModel::getAllRole();
        return View::fetch('index',['roleData'=>$roleData]);
    }

    /*
     * 删除角色
     * */
    public function delete(){
        if($this->request->isAjax()){
            $id = $this->request->post('id');
            try{
                $model = $this->findModel($id);
                $model->delete();
            }catch (\Exception $ex){
                return JsonService::error($ex->getMessage());
            }
            return JsonService::success('删除成功');
        }
    }

    /**
     * 得到模型
     *
     * @param       $id
     * @param array $where
     *
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public function findModel($id,$where = [])
    {
        if (empty($id) && empty($where)) {
            throw new \think\exception\RouteNotFoundException();
        }

        return RoleModel::findModel($id, $where);
    }
}